import { _decorator, Component, Node, Sprite, UITransform, Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enums';
import { ISpikes } from '../../Levels';
import { StateMachine } from '../../Base/StateMachine';
import { randomByLen } from '../../Utils';
import { TILE_WIDTH, TILE_HEIGHT } from '../Tile/TileManager';
import { SpikesStateMachine } from './SpikesStateMachine';
import EventManager from '../../Runtime/EventManager';
import DataManager from '../../Runtime/DataManager';
const { ccclass, property } = _decorator;



@ccclass('SpikesManager')
export class SpikesManager extends Component {
    id:string = randomByLen(12)
    x: number = 0;
    y: number = 0;

    fsm: StateMachine;

    private _count: number;
    private _totalCount: number;
    private type: ENTITY_TYPE_ENUM

    public get count() {
        return this._count;
    }
    public set count(newCount: number) {
        this._count = newCount;
        this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT,  newCount); // 设置状态机的参数
    }
    public get totalCount(): number {
        return this._totalCount;
    }
    public set totalCount(newTotalCount: number) {
        this._totalCount = newTotalCount;
        this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT,  newTotalCount); // 设置状态机的参数
    }


    async init(params:ISpikes) {
        
        const sprite = this.addComponent(Sprite);
        sprite.sizeMode = Sprite.SizeMode.CUSTOM;
        const transform = this.getComponent(UITransform);
        transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4);


        this.fsm = this.addComponent(SpikesStateMachine);
        await this.fsm.init();

        this.x = params.x;
        this.y = params.y;
        this.type = params.type;
        this.totalCount = SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[this.type];
        this.count = params.count;

        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onLoop, this)

    }

    protected update(dt: number): void {
        this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)
    }

    protected onDestroy(): void {
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onLoop)
    }

    onLoop(){
        if(this.count === this.totalCount) {
            this.count = 1
        } else {
            this.count++;
        }

        this.onAttack();
    }


    backZero(){
        this.count = 0;
    }

    onAttack() {
        if(!DataManager.Instance.player){
            return;
        }

        const {x:playerX,y:playerY} = DataManager.Instance.player;
        if(this.x===playerX && this.y === playerY && this.count === this.totalCount) {
            EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER, ENTITY_STATE_ENUM.DEATH)
        }
    }



}


